package mmc.ui;

import static mmc.resource.Text12p.glText12p;
import static org.lwjgl.opengl.GL11.glColor3f;


import mmc.Client;
import mmc.MovableEngineObject;
import mmc.Player;
import mmc.objects.Mob;
import mmc.objects.RemotePlayer;
import mmc.stats.StandardStats;

import org.lwjgl.opengl.GL11;

public class UnitFrame extends Component {
	private Client client;
	
	public UnitFrame(float pos_x, float pos_y, float size_x, float size_y, Client client) {
		super(pos_x, pos_y, size_x, size_y);
		this.client = client;		
	}

	public void drawFrame(String text, float hp, float energy, float dx, float dy){
		//player frame
		glColor3f(1, 0,0); 
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x+dx, y+dy);
		GL11.glVertex2f(x+dx+width, y+dy);
		GL11.glVertex2f(x+dx+width, y+dy+height);
		GL11.glVertex2f(x+dx, y+dy+height);
		GL11.glEnd();			
		
		glColor3f(0, 1.0f, 0); 
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x+dx+width*0.01f, y+dy+height*0.05f);
		GL11.glVertex2f(x+dx+width*0.99f*hp, y+dy+height*0.05f);
		GL11.glVertex2f(x+dx+width*0.99f*hp, y+dy+height*0.95f);
		GL11.glVertex2f(x+dx+width*0.01f, y+dy+height*0.95f);
		GL11.glEnd();				
		glColor3f(0,0,0);
		glText12p(text, x+dx+width*0.3f, y+dy+height*0.2f);
		
		if(energy == -1) return;
		//Energy bar
		glColor3f(1, 0,0); 
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x+dx, y+dy+height);
		GL11.glVertex2f(x+dx+width, y+dy+height);
		GL11.glVertex2f(x+dx+width, y+dy+height*2);
		GL11.glVertex2f(x+dx, y+dy+height*2);
		GL11.glEnd();			

		glColor3f(0, 0, 1.0f); 
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x+dx+width*0.01f, y+dy+height*0.05f+height);
		GL11.glVertex2f(x+dx+width*0.99f*energy, y+dy+height*0.05f+height);
		GL11.glVertex2f(x+dx+width*0.99f*energy, y+dy+height*0.95f+height);
		GL11.glVertex2f(x+dx+width*0.01f, y+dy+height*0.95f+height);
		GL11.glEnd();				
	}
	
	@Override
	public void render() {
		if(client.getScene() == null) return;
		
		Player player = client.getScene().player;
		float hp = player.stats.getPercentalValue(StandardStats.HP);
		hp = Math.max(hp,0);
		float energy = player.stats.getPercentalValue(StandardStats.EN);
		energy = Math.max(energy,0);
		
		drawFrame(player.getName()+": " + player.stats.getValue(StandardStats.HP), hp, energy, 0, 0);
		
		
		
		if(player.target != null)
		{
			MovableEngineObject target = (MovableEngineObject) player.target;
			float dx = width*1.1f;
			float dy = 0;
						
			//target frame
			float hp_target = target.stats.getPercentalValue(StandardStats.HP);
			hp_target = Math.max(hp_target,0);

			
				
			glColor3f(0,0,0);
			if(target instanceof Mob){
				drawFrame("Mob "+ target.id, hp_target, -1, dx, dy);
				//glText12p("Mob "+ target.id, x+width*0.3f+dx, y+height*0.2f+dy);
				if(((Mob)target).target != -1){
					RemotePlayer target_target = (RemotePlayer)client.getScene().objects.get(((Mob)target).target);
					if(target_target != null )
						drawTargetOfTarget(target_target.name, target_target.health, dx ,dy);
				}
			}
			else{
				//glText12p(((RemotePlayer)target).name, x+width*0.3f+dx, y+height*0.2f+dy);	
				drawFrame(((RemotePlayer)target).name, hp_target, -1, dx, dy);
				if(((RemotePlayer)target).target != -1){
					RemotePlayer target_target = (RemotePlayer)client.getScene().objects.get(((Mob)target).target);
					drawTargetOfTarget(target_target.name, target_target.health, dx ,dy);
				}
			}				
		}
	}

	public void drawTargetOfTarget(String name, float health, float dx, float dy){
		float ddx = dx + 0.1f;
		float ddy = dy + 0.06f;
		
		//target of target frame
		float hp_target_target = health;
		glColor3f(1, 0,0); 
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x+ddx, y+ddy);
		GL11.glVertex2f(x+width+ddx, y+ddy);
		GL11.glVertex2f(x+width+ddx, y+height+ddy);
		GL11.glVertex2f(x+ddx, y+height+ddy);
		GL11.glEnd();						
		glColor3f(0, 1.0f, 0); 
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x+width*0.01f+ddx, y+height*0.05f+ddy);
		GL11.glVertex2f(x+width*0.99f*hp_target_target+ddx, y+height*0.05f+ddy);
		GL11.glVertex2f(x+width*0.99f*hp_target_target+ddx, y+height*0.95f+ddy);
		GL11.glVertex2f(x+width*0.01f+ddx, y+height*0.95f+ddy);
		GL11.glEnd();
		glColor3f(0,0,0);
		glText12p(name, x+width*0.3f+ddx, y+height*0.2f+ddy);
	}
}
